package com.net.oil.main.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.main.bean.BannerShopBean;
import com.net.oil.main.bean.ShopListBean;
import com.net.oil.main.contract.ShopContract;
import com.net.oil.main.presenter.ShopPresenter;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/3/31:11:56
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class ShopModel extends BaseModel implements ShopContract.Model {

    private ShopPresenter presenter;

    public ShopModel(ShopPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    /**
     * 获取商家列表
     * @param province  省
     * @param city  城市
     * @param county    县/区
     * @param name  商家名称模糊查询
     * @param lng   当前位置纬度
     * @param lat   当前位置经度
     * @param page  页码
     * @param rows  分页大小
     */
    @Override
    public void getShopList(String province, String city, String county, String name, double lng, double lat, final int page, int rows) {
        AppHttpClient.ApiService().getShopList(province, city, county, name, lng, lat, page, rows)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ShopListBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean<ShopListBean> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getShopListSuccess(tResultBean.getData());
                        }else {
                            presenter.getShopListFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.getShopListFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void getBannerShopList() {
        AppHttpClient.ApiService().getBannerShopList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<BannerShopBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<List<BannerShopBean>> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getBannerShopListSuccess(tResultBean.getData());
                        }else {
                            presenter.getBannerShopListFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getBannerShopListFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
